﻿using JYH.Data;
using JYH.DataAccess;
using JYH.Model;
using NetWorkLayer;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace JYH.Business
{
    /// <summary>
    /// 电子料架控制层
    /// </summary>
    public class NetWorkLayerHelper
    {
        /// <summary>
        /// 电子料架初始化检测
        /// </summary>
        /// <returns></returns>

        public static bool InitComNetClient()
        {
            Database db = DB.D();
            bool result = false;
            List<string> listIP = new List<string>();
            Sql sql = new Sql();
            List<BasRackInfo> RackInfos = new List<BasRackInfo>();
            sql.Append("Select BasRackInfo.* From  BasRackInfo ");
            sql.OrderBy("BasRackInfo.RackNO");
            RackInfos = db.Fetch<BasRackInfo>(sql);
            if (RackInfos.Count > 0)
            {
                foreach (BasRackInfo rackInfo in RackInfos)
                {
                    listIP.Add(rackInfo.RackIP + ":" + rackInfo.PortNO + "_" + rackInfo.RackNO + ":" + ((int)(rackInfo.ProsCons * rackInfo.LayerQty)).ToString());
                }
            }
            if (listIP.Count > 0)
            {
                result = ComNetClient.InitIPAddress(listIP, "", true);
            }
            return result;
        }

        public static void LedControl(int nColor, List<string> list, bool bLight, bool bLightHouse)
        {
            Thread thread = new Thread(() => MutiShelfLEDBatchControl(nColor, list, bLight, bLightHouse));
            thread.Start();
        }

        public static bool MutiShelfLEDBatchControl(int nColor, List<string> list, bool bLight, bool bLightHouse)
        {
           
            return ComNetClient.MutiShelfLEDBatchControl(nColor, list, bLight, bLightHouse);
        }

        /// <summary>
        ///  批量多料架多灯珠亮灯
        /// </summary>
        /// <param name="nColor">亮灯颜色</param>
        /// <param name="list">LampCode集合</param>
        /// <param name="bLight">灯珠是否亮灯</param>
        /// <param name="bLightHouse">灯塔是否亮灯</param>
        public static bool OnLampCode(int nColor, List<string> list, bool bLight, bool bLightHouse)
        {
            try
            {
                //初始化料架
                InitComNetClient();
                Thread.Sleep(200);
                //ComNetClient.MutiShelfLEDBatchControl(nColor, list, bLight, bLightHouse);
                //等待作业，显示黄色
                LedControl(nColor, list, bLight, bLightHouse);

                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}